using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ControlCenter.Data;
using ControlCenter.Model;
using ControlCenter.Model.BizEntities;
using ControlCenter.Model.WebApiDto;
using MingYuanYun.Context.Abstractions;
using MingYuanYun.Data.Abstractions;

namespace ControlCenter.Operation
{
    /// <summary>
    /// 机器人操作类
    /// </summary>
    public class RobotOperation : OperationBase, IRobotOperation
    {
        private readonly IRobotRepository _robotRepository;

        /// <summary>
        /// 类初始化
        /// </summary>
        /// <param name="context"></param>
        /// <param name="unitOfWork"></param>
        public RobotOperation(IServiceContext context, IUnitOfWork unitOfWork) : base(context, unitOfWork)
        {
            _robotRepository = GetRepository<IRobotRepository>();
        }

        /// <summary>
        /// 根据条件查询机器人数据，返回分页数据
        /// </summary>
        /// <param name="paramDto"></param>
        /// <returns></returns>
        public async Task<PagedData<RobotInfoDto>> Query(RobotQueryDto paramDto)
        {
            PagedData<RobotInfoDto> pagedData = new PagedData<RobotInfoDto>();
            var packages = await _robotRepository.Query(paramDto);
            pagedData.Data = packages.Skip((paramDto.PageIndex - 1) * paramDto.PageSize).Take(paramDto.PageSize).ToList();
            pagedData.TotalCount = packages.Count();
            return pagedData;
        }

        /// <summary>
        /// 通过ID查找机器人
        /// </summary>
        /// <param name="robotId"></param>
        /// <returns></returns>
        public async Task<RobotInfo> Find(Guid robotId)
        {
            return await _robotRepository.Find(robotId);
        }

        /// <summary>
        /// 新增或更新机器人信息
        /// </summary>
        /// <param name="robotInfo"></param>
        /// <returns></returns>
        public async Task<bool> UpdateOrSave(RobotInfo robotInfo)
        {
            return await _robotRepository.UpdateOrSave(robotInfo);
        }

        /// <summary>
        /// 更新机器人状态
        /// </summary>
        /// <param name="robotId"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public async Task<bool> UpdateRobotStatus(Guid robotId, int status)
        {
            return await _robotRepository.UpdateRobotStatus(robotId, status);
        }

        /// <summary>
        /// 删除机器人
        /// </summary>
        /// <param name="robotId"></param>
        /// <returns></returns>
        public async Task<bool> DeleteRobot(Guid robotId)
        {
            return await _robotRepository.DeleteRobot(robotId);
        }
    }
}
